package com.yanchangapp.ui.activity._n_manage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.yanchangapp.R;
import com.yanchangapp.adapter._n_manage.AttachmentAdapter;
import com.yanchangapp.adapter._n_manage.OperateBookAdapter;
import com.yanchangapp.commom.LoadingType;
import com.yanchangapp.model.bean.AttachBean;
import com.yanchangapp.model.bean.OperateBookBean;
import com.yanchangapp.model.response.WorkItemDetailResponse;
import com.yanchangapp.ui.activity.mail.AttachmentShowActivity;
import com.yanchangapp.ui.presenter.WorkItemDtAtPresenter;
import com.yanchangapp.ui.view.WorkItemDtAtView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.widget.NiceNestedScrollView;
import com.zxl.baselib.widget.YCLoadingView;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
/**
 * page: 我的工作项 详情页面...
 * <p>
 * 1.初始化根据ID查询工作项详情页面....
 * 2.生成今日计划... 页面进行刷新....
 *
 */
public class WorkItemDetailActivity extends BaseActivity<WorkItemDtAtView, WorkItemDtAtPresenter> implements WorkItemDtAtView {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.tvTime)
    TextView tvTime;
    @BindView(R.id.nestScrollview)
    NiceNestedScrollView nestScrollview;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.ycLoading)
    YCLoadingView ycLoading;
    private static final String WORK_ID = "WORK_ID";
    @BindView(R.id.rvOperateBook)
    RecyclerView rvOperateBook;
    @BindView(R.id.llAttach)
    LinearLayout llAttach;
    @BindView(R.id.rvAttachmentLists)
    RecyclerView rvAttachmentLists;
    private long workId;
    private OperateBookAdapter mOperateBookAdapter;
    private List<OperateBookBean> mOperateBookBeans = new ArrayList<>();
    private AttachmentAdapter mAttachmentAdapter;
    private List<AttachBean> mAttachBeans = new ArrayList<>();

    private void setLoadingState(LoadingType loadingType) {
        switch (loadingType) {
            case LOADING:
                if (ycLoading.getVisibility() == View.GONE) {
                    ycLoading.setVisibility(View.VISIBLE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE) {
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (nestScrollview.getVisibility() == View.VISIBLE) {
                    nestScrollview.setVisibility(View.GONE);
                }
                break;
            case SUCCESS:
                if (ycLoading.getVisibility() == View.VISIBLE) {
                    ycLoading.setVisibility(View.GONE);
                }


                if (llErrorNetwork.getVisibility() == View.VISIBLE) {
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (nestScrollview.getVisibility() == View.GONE) {
                    nestScrollview.setVisibility(View.VISIBLE);
                }

                break;
            case ERROR:
                if (ycLoading.getVisibility() == View.VISIBLE) {
                    ycLoading.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.GONE) {
                    llErrorNetwork.setVisibility(View.VISIBLE);
                }

                // 设置布局隐藏
                if (nestScrollview.getVisibility() == View.VISIBLE) {
                    nestScrollview.setVisibility(View.GONE);
                }
                break;
        }
    }

    public static void show(Activity activity, long workId) {
        Intent intent = new Intent(activity, WorkItemDetailActivity.class);
        intent.putExtra(WORK_ID, workId);
        activity.startActivity(intent);
    }

    @Override
    protected void init() {
        workId = getIntent().getLongExtra(WORK_ID, -1);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_work_item_detail;
    }

    @Override
    protected WorkItemDtAtPresenter createPresenter() {
        return new WorkItemDtAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("我的工作项");
        initAdapter();
    }

    private void initAdapter() {
        LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        manager.setSmoothScrollbarEnabled(true);
        manager.setAutoMeasureEnabled(true);
        rvOperateBook.setLayoutManager(manager);
        rvOperateBook.setHasFixedSize(true);
        rvOperateBook.setNestedScrollingEnabled(false);
        mOperateBookAdapter = new OperateBookAdapter(mOperateBookBeans);
        rvOperateBook.setAdapter(mOperateBookAdapter);

        // 设置附件的adapter类
        LinearLayoutManager attachmentManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        attachmentManager.setSmoothScrollbarEnabled(true);
        attachmentManager.setAutoMeasureEnabled(true);
        rvAttachmentLists.setLayoutManager(attachmentManager);
        mAttachmentAdapter = new AttachmentAdapter(mAttachBeans);
        rvAttachmentLists.setAdapter(mAttachmentAdapter);
    }

    @Override
    protected void initData() {
        setLoadingState(LoadingType.LOADING);
        mPresenter.queryWorkItemDetail(workId);
    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(v -> finish());

        mAttachmentAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.tvLook){
                AttachmentShowActivity.show(WorkItemDetailActivity.this,mAttachBeans.get(position).getAttachmentUrl());
            }
        });

        btnNextRefresh.setOnClickListener(v -> initData());
    }

    @Override
    public void queryWorkItemDetailSuccess(WorkItemDetailResponse workItemDetailResponse) {
        setLoadingState(LoadingType.SUCCESS);
        mOperateBookBeans.clear();
        mOperateBookBeans.addAll(workItemDetailResponse.getOperationList());
        mOperateBookAdapter.notifyDataSetChanged();
        tvToolbarTitle.setText(workItemDetailResponse.getTitle());
        if (workItemDetailResponse.getStandardTime() == null){
            tvTime.setText("暂无");
        }else {
            tvTime.setText(getString(R.string.strand_time,workItemDetailResponse.getStandardTime(),workItemDetailResponse.getStandardTimeTypeName()));
        }
        if ((workItemDetailResponse.getAttachments() != null)
        && (workItemDetailResponse.getAttachments().size() != 0)){
            mAttachBeans.addAll(workItemDetailResponse.getAttachments());
            mAttachmentAdapter.notifyDataSetChanged();
        }else {
            llAttach.setVisibility(View.GONE);
        }
    }

    @Override
    public void queryWorkItemDetailFailure(String err) {
        setLoadingState(LoadingType.ERROR);
        UIUtils.showToast(err);
    }
}
